VERSION 5.00
Begin VB.Form Form1
BorderStyle = 3 'Fixed Dialog
Caption = "Tower"
ClientHeight = 5100
ClientLeft = 45
ClientTop = 330
ClientWidth = 7305
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 5100
ScaleWidth = 7305
ShowInTaskbar = 0 'False
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command1
Caption = "/\"
Height = 495
Index = 2
Left = 5280
TabIndex = 2
Top = 4320
Width = 855
End
Begin VB.CommandButton Command1
Caption = "/\"
Height = 495
Index = 1
Left = 3360
TabIndex = 1
Top = 4320
Width = 855
End
Begin VB.CommandButton Command1
Caption = "/\"
Height = 495
Index = 0
Left = 1320
TabIndex = 0
Top = 4320
Width = 855
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim Pegs As Integer
Dim From2 As Byte
' Peg Layer
Dim Tower() As Byte
Private Sub Command1_Click(Index As Integer)
If From2 = 0 Then
From2 = Index + 1
Command1(Index).Caption = "/||\"
Exit Sub
End If
If From2 = Index + 1 Then
From2 = 0
Command1(Index).Caption = "/\"
Exit Sub
End If
If IsOK(From2, Index + 1) Then
moveit From2, Index + 1
Draw
Command1(From2 - 1).Caption = "/\"
From2 = 0
If HasWon Then
MsgBox "You Have WON"
Form_Load
End If
Else
MsgBox "Can't do that"
Command1(From2 - 1).Caption = "/\"
From2 = 0
End If
End Sub
Private Sub Form_Load()
Pegs = InputBox("How many pegs?", , "3")
ReDim Tower(1 To 3, 1 To Pegs)
For i = 1 To Pegs
Tower(1, i) = Pegs + 1 - i
Next
Form_Paint
End Sub
Private Function IsOK(From1 As Byte, To1 As Byte) As Boolean
Dim TopLayer1 As Byte
Dim TopLayer2 As Byte
For i = 1 To UBound(Tower, 2)
If Tower(From1, i) = 0 Then
TopLayer1 = i - 1
Exit For
End If
TopLayer1 = i
Next
For i = 1 To UBound(Tower, 2)
If Tower(To1, i) = 0 Then
TopLayer2 = i - 1
Exit For
End If
TopLayer2 = i
Next
On Error Resume Next
If Tower(To1, TopLayer2) > Tower(From1, TopLayer1) Then
IsOK = True
Else
IsOK = False
End If
End Function
Private Function moveit(From1 As Byte, To1 As Byte) As Boolean
Dim TopLayer1 As Byte
Dim TopLayer2 As Byte
For i = 1 To UBound(Tower, 2)
If Tower(From1, i) = 0 Then
TopLayer1 = i - 1
Exit For
End If
TopLayer1 = i
Next
For i = 1 To UBound(Tower, 2)
If Tower(To1, i) = 0 Then
TopLayer2 = i - 1
Exit For
End If
TopLayer2 = i
Next
On Error Resume Next
If Tower(To1, TopLayer2) > Tower(From1, TopLayer1) Then
Tower(To1, TopLayer2 + 1) = Tower(From1, TopLayer1)
Tower(From1, TopLayer1) = 0
moveit = True
Else
moveit = False
End If
End Function
Private Sub Draw()
Cls
'draw towers
DrawWidth = 10
ForeColor = QBColor(0)
Line (Width * 0.25, Height * 0.25)-(Width * 0.25, Height * 0.75)
Line (Width * 0.5, Height * 0.25)-(Width * 0.5, Height * 0.75)
Line (Width * 0.75, Height * 0.25)-(Width * 0.75, Height * 0.75)
ForeColor = QBColor(10)
For x = 1 To 3
For s = 1 To Pegs
If Tower(x, s) <> 0 Then
Line (Width * ((0.25 * x - 0.02 * Tower(x, s))), Height * (0.75 - (0.5 / (Pegs - 1)) * (s - 1)))-(Width * (0.25 * x + 0.02 * Tower(x, s)), Height * (0.75 - (0.5 / (Pegs - 1)) * (s - 1)))
End If
Next
Next
End Sub
Private Sub Form_Paint()
Draw
End Sub
Private Function HasWon() As Boolean
For i = 1 To UBound(Tower, 2)
If Pegs + 1 - i <> Tower(3, i) Then
HasWon = False
Exit Function
End If
Next
HasWon = True
End Function